/*
 * Copyright (C) 2019 Peng fei Pan <panpfpanpf@outlook.me>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package me.panpf.sketch.util;

import me.panpf.sketch.drawable.MiPixmapElement;
import me.panpf.sketch.zoom.PixmapBuild;
import ohos.agp.components.element.Element;
import ohos.agp.components.element.PixelMapElement;
import ohos.agp.render.BlendMode;
import ohos.agp.render.Canvas;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;
import org.jetbrains.annotations.Nullable;

public class DrawableWrapper extends MiPixmapElement implements Element.OnChangeListener {

    @Nullable
    private Element wrappedDrawable;

    public DrawableWrapper(@Nullable PixelMapElement drawable, PixmapBuild imageSource) {
        super(imageSource);
        setWrappedDrawable(drawable);
    }


    @Override
    public void drawToCanvas(Canvas canvas) {
        if (wrappedDrawable != null) {
            wrappedDrawable.drawToCanvas(canvas);
        }
    }


    @Override
    public void setAlpha(int alpha) {
        if (wrappedDrawable != null) {
            wrappedDrawable.setAlpha(alpha);
        }
    }


    @Override
    public boolean isStateful() {
        return wrappedDrawable != null ? wrappedDrawable.isStateful() : super.isStateful();
    }


    @Override
    public Element getCurrentElement() {
        return wrappedDrawable != null ? wrappedDrawable.getCurrentElement() : super.getCurrentElement();
    }

    @Override
    public boolean setVisible(boolean visible, boolean restart) {
        return super.setVisible(visible, restart) || (wrappedDrawable != null && wrappedDrawable.setVisible(visible, restart));
    }

    @Override
    public int getMinWidth() {
        return wrappedDrawable != null ? wrappedDrawable.getMinWidth() : super.getMinWidth();
    }


    @Override
    public int getMinHeight() {
        return wrappedDrawable != null ? wrappedDrawable.getMinHeight() : super.getMinHeight();
    }

    @Override
    public boolean isInheritDirection() {
        return wrappedDrawable != null && wrappedDrawable.isInheritDirection();
    }

    @Override
    public void setInheritDirection(boolean mirrored) {
        if (wrappedDrawable != null) {
            wrappedDrawable.setInheritDirection(mirrored);
        }
    }

    @Override
    public void setStateColorList(int[][] colorState, int[] colors) {
        if (wrappedDrawable != null) {
            wrappedDrawable.setStateColorList(colorState, colors);
        }
    }


    @Override
    public void setStateColorMode(BlendMode tintMode) {
        if (wrappedDrawable != null) {
            wrappedDrawable.setStateColorMode(tintMode);
        }
    }


    @Nullable
    public Element getWrappedDrawable() {
        return wrappedDrawable;
    }

    public void setWrappedDrawable(@Nullable Element drawable) {
        if (drawable == this) {
            return;
        }

        if (wrappedDrawable != null) {
            wrappedDrawable.setCallback(null);
        }

        this.wrappedDrawable = drawable;

        if (drawable != null) {
            drawable.setCallback(this);
        }
    }

    @Override
    public void onChange(Element element) {

    }
}